/*
 * @Description: Rolin's code edit
 * @Author: Rolin-Code
 * @Date: 2021-06-29 15:42:25
 * @LastEditors: Rolin
 * @Code-Function-What do you want to do: 
 * @FilePath: \创新创业案例-区块链即时通讯d:\Project\WuhuServiceSystem\wuhuflight\js\proMeal.js
 */
var mainFood,secFood,drinkFood;
var mainFoodID,secFoodID,drinkFoodID;
var mainFoodPrice = 0.00,secFoodPrice = 0.00,drinkFoodPrice = 0.00;
var pay = 0;

function returnFree(){
    window.location.href="/wss/features/meals/free";
}


function init_page(){
    //选餐控制函数
    $('#select').on("change",function(){
        returnFree();
    });

    $('#btn').attr('disabled',true);
    $('.count-text').html("未选择任何餐品");
    
    var main = $('#Meal1');
    var second = $('#Meal2');
    var drink = $('#Meal3');

    second.css("display","none");
    drink.css("display","none");


    $('#MealChoice-1').on("click",function(){
        main.css("display","flex");
        second.css("display","none");
        drink.css("display","none");
    });

    $('#MealChoice-2').on("click",function(){
        main.css("display","none");
        second.css("display","flex");
        drink.css("display","none");
    });

    $('#MealChoice-3').on("click",function(){
        main.css("display","none");
        second.css("display","none");
        drink.css("display","flex");
    });

    $('.food').on("click",function (){
        var id = $(this).attr("name"); //获取菜品ID
        var divID = $(this).attr('id');
        var type = $(this).parent().attr("id"); //父类DIV的ID
        
        if(type == "Meal1") {
            $("#MealChoice-1").css("background","url('"+$("#"+divID+" img").attr("src")+"') no-repeat");
            $("#MealChoice-1").css("background-size","100% 100%");
            $("#MealChoice-1").css("color","white");
            $("#MealChoice-1").html("主食<br />"+$("#food"+id+" p").html());
            mainFood = $("#food"+id+" p").html();
            mainFoodID = id;
            mainFoodPrice = parseFloat($("#food"+id+" div").html().replace("￥",""));
        }else if(type == "Meal2"){
            $("#MealChoice-2").css("background","url('"+$("#"+divID+" img").attr("src")+"') no-repeat");
            $("#MealChoice-2").css("background-size","100% 100%");
            $("#MealChoice-2").css("color","white");
            $("#MealChoice-2").html("副食<br />"+$("#food"+id+" p").html());
            secFood = $("#food"+id+" p").html();
            secFoodID = id;
            secFoodPrice = parseFloat($("#food"+id+" div").html().replace("￥",""));
        } else if(type == "Meal3"){
            $("#MealChoice-3").css("background","url('"+$("#"+divID+" img").attr("src")+"') no-repeat");
            $("#MealChoice-3").css("background-size","100% 100%");
            $("#MealChoice-3").css("color","white");
            $("#MealChoice-3").html("饮品<br />"+$("#food"+id+" p").html());
            drinkFood = $("#food"+id+" p").html();
            drinkFoodID = id;
            drinkFoodPrice = parseFloat($("#food"+id+" div").html().replace("￥",""));
        }

        if(mainFood && secFood && drinkFood)    $('#btn').attr('disabled',false);
        pay = mainFoodPrice+secFoodPrice+drinkFoodPrice;
        $('.count-text').html("应付价格："+pay+"元");
    });
}
function submitOrder(user,ticket,url) {
    const con = confirm("你将购买"+mainFood+"、"+secFood+"和"+drinkFood+"\n即将支付"+pay+"元\n是否购买？");
    if(con){
        $.ajax({
            url:url,
            type:"POST",
            contentType: "application/json;charset=UTF-8",
            dataType:"json",
            data:JSON.stringify({
                userID:user,
                ticketID:ticket,
                food1:mainFoodID,
                food2:secFoodID,
                food3:drinkFoodID
            }),
            success:function (data) {
                console.log(data);
                if(data.code == 0){
                    alert("购买成功!等待飞机起飞时将送餐到坐！");
                    window.location.href="/wss/features/doing/center";
                }else{
                    alert(data.message);
                }
            }
        });
    }
}